package com.dpj.n4.deadlock.v3;

import lombok.extern.slf4j.Slf4j;

/*
    定位死锁的方法
        1.jps + jstack
        2.jconsole
 */
@Slf4j(topic = "c.TestDeadLock")
public class TestDeadLock {
    public static void main(String[] args) {
        Object A=new Object();
        Object B=new Object();
        Thread t1=new Thread(()->{
            synchronized (A){
                try {
                    log.debug("lockA");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (B){
                    log.debug("lockB");
                    log.debug("操作");
                }
            }
        },"t1");
        Thread t2=new Thread(()->{
            synchronized (B){
                log.debug("lockB");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (A){
                    log.debug("lock A");
                    log.debug("操作");
                }
            }
        },"t2");
        t1.start();
        t2.start();
    }
}
